package com.freesky.provider.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freesky.common.dao.entity.Item;
import com.freesky.common.dao.entity.Panel;
import com.freesky.common.dao.entity.PanelContent;
import com.freesky.common.enums.PanelPositionEnum;
import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.enums.StateEnum;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.pojo.ZTreeNode;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.provider.content.service.ICommonService;
import com.freesky.provider.content.service.IHomePanelContentService;
import com.freesky.provider.content.util.VOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Freesky
 */
@Service
public class HomePanelContentServiceImpl implements IHomePanelContentService {

    private final static Logger logger= LoggerFactory.getLogger(HomePanelContentServiceImpl.class);

    @Autowired
    private IRedisTemplate redisTemplate;
    @Autowired
    private ICommonService commonService;

    @Override
    public List<ZTreeNode> treeList() {
        List<Panel> panelList = new Panel().selectList(new EntityWrapper().eq("position", PanelPositionEnum.HOME_PAGE));
        //首页板块
        List<ZTreeNode> list = new ArrayList<>();
        for (Panel panel : panelList) {
            ZTreeNode zTreeNode = VOUtil.changePanelToZTreeNode(panel);
            list.add(zTreeNode);
        }

        return list;
    }

    @Override
    public DataTablesResult list(int panelId) {
        return commonService.getPanelContentListByPanelId(panelId);
    }

    @Override
    public JSONResponse delete(int[] ids) {
        return commonService.delete(ids);
    }

    @Override
    public JSONResponse update(PanelContent panelContent) {
        return commonService.updateContent(panelContent);
    }

    @Override
    public JSONResponse getHomeContent() {
        List<Panel> list ;

        //查询缓存
        try {
            //有缓存则读取
            list = redisTemplate.getCacheList(RedisKeyEnum.HOME_PANEL_CONTENT.getKey(), Panel.class);
            if (list != null) {
                logger.info("读取了首页缓存");
                return JSONResponseDiretor.buildSuccessJSONResponse(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //没有缓存
        list = new Panel().selectList(new EntityWrapper().eq("position", PanelPositionEnum.HOME_PAGE).eq("state", StateEnum.INUSE).orderBy("sort_order", true));


        for (Panel panel : list) {
            List<PanelContent> contentList = new PanelContent().selectList(new EntityWrapper().eq("panel_id", panel.getId()).orderBy("sort_order", true));
            for (PanelContent content : contentList) {
                if (content.getItemId() != null) {
                    Item item = new Item();
                    item.setId(content.getItemId());
                    item = item.selectById();
                    content.setItemName(item.getTitle());
                    content.setSalePrice(item.getPrice());
                    content.setSubTitle(item.getSellPoint());
                }
            }

            panel.setPanelContents(contentList);
        }

        //把结果添加至缓存
        if (list != null) {
            redisTemplate.putCache(RedisKeyEnum.HOME_PANEL_CONTENT.getKey(), JSON.toJSON(list));
            logger.info("添加了首页缓存");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(list);
    }

}
